﻿Imports Microsoft.VisualBasic

Public Class GridViewTemplate
	Inherits WebBase
	Implements ITemplate

	Public Enum ItemType
		Edit
		Delete
		Selection
		Display
		Label
		Header
	End Enum

	Private _templateType As ListItemType
	Private _columnName As String
	Private _itemType As ItemType

	Public Sub New(ByVal type As ListItemType, ByVal colname As String, ByVal itemType As ItemType)
		_templateType = type
		_columnName = colname
		_itemType = itemType
	End Sub

	Private Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn

		Select Case _templateType

			Case ListItemType.Header
				Dim lbl As New Label()
				lbl.Text = _columnName
				container.Controls.Add(lbl)

				Exit Select
			Case ListItemType.Item

				Select Case _itemType

					Case ItemType.Display
						Dim btn As New Button
						btn.ID = "ux_Display"
						btn.CausesValidation = False
						btn.CommandName = "DisplayButton_Command"
						btn.Text = "Display"
						btn.CssClass = "BUTTON"
						btn.Attributes.Add("onmouseover", "this.className='BUTTONOVER'")
						btn.Attributes.Add("onmouseout", "this.className='BUTTONOUT'")
						AddHandler btn.DataBinding, AddressOf ButtonItem_DataBinding
						AddHandler btn.Command, AddressOf DisplayButton_Command

						container.Controls.Add(btn)

						Exit Select
					Case ItemType.Label
						Dim lblItem As New Label()
						AddHandler lblItem.DataBinding, AddressOf lblItem_DataBinding
						container.Controls.Add(lblItem)

						Exit Select
				End Select

				Exit Select
			Case ListItemType.EditItem

				Exit Select
			Case ListItemType.Footer

				Exit Select
		End Select
	End Sub

	Private Sub lblItem_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
		Dim labelData As Label = DirectCast(sender, Label)

		Dim container As GridViewRow = DirectCast(labelData.NamingContainer, GridViewRow)
		Dim dataValue As Object = DataBinder.Eval(container.DataItem, _columnName)

		If Not (dataValue.Equals(DBNull.Value)) Then
			labelData.Text = dataValue.ToString()
		End If
	End Sub

	Protected Overloads Sub DisplayButton_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
		Dim page As String = AppPage
		Dim FormID As String = HttpContext.Current.Request.QueryString("FormID")
		Dim FormResponseID As String = e.CommandArgument.ToString()

		HttpContext.Current.Response.Redirect(String.Format("{0}.aspx?pid=form_formviewer&formid={1}&formresponseid={2}", page, FormID, FormResponseID))
	End Sub

	Private Sub ButtonItem_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
		Dim btn As Button = DirectCast(sender, Button)

		Dim container As GridViewRow = DirectCast(btn.NamingContainer, GridViewRow)
		Dim dataValue As Object = DataBinder.Eval(container.DataItem, _columnName)

		If Not (dataValue.Equals(DBNull.Value)) Then
			btn.CommandArgument = dataValue.ToString()
		End If
	End Sub

End Class